<?php
date_default_timezone_set('prc');
header("Content-type: text/html; charset=utf-8");

//$con = @mysql_connect("59.188.255.28","sq_vferth","wulang00");
//$con = @mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
$con = @mysql_connect("localhost".':'."3306","root","mysql");

if (!$con)
  {
  die('亲，由于网络太忙，您的订单未能成功提交，请您稍后重试。');
  }

mysql_select_db("chiguoguo", $con);
//mysql_select_db(SAE_MYSQL_DB, $con);
mysql_query("set names 'UTF8'");
//mysql_query("SET NAMES 'GBK'"); 


$date=date("Y-m-d H:i:s");

$sql="INSERT INTO `gift_order` (client_name, address, phone_number, amount_t1, amount_t2, amount_t3, insert_date) VALUES('$_POST[client_name]','$_POST[address]','$_POST[phone_number]', '$_POST[amount_t1]', '$_POST[amount_t2]', '$_POST[amount_t3]', '$date')";

$amount_array=array(0=>$_POST[amount_t1],1=>$_POST[amount_t2],2=>$_POST[amount_t3]);

$total_amount=array_sum($amount_array); 

if ($total_amount > 10)
	{
		die('亲，每个人最多定10盒哦！请重新下单，谢谢！');
	}

// get left amount
$result = mysql_query("SELECT amount FROM `gift`");
$row = mysql_fetch_array($result);
$left_amount=$row['amount'];
$new_left_amount=$left_amount-$total_amount;

if ($new_left_amount < 0 )
	{
		die('亲，今日的剩余量已不够您的订单需求，请您核对后再重新下单，谢谢您的合作！');
	}
	
if (!mysql_query($sql,$con))
  {
		die('亲，由于网络太忙，您的订单未能成功保存，请您稍后重试。');
  }else{
		mysql_query("UPDATE `gift` set amount='$new_left_amount'");
  }

echo "<h1>感谢您对吃果果的支持，您的订单已成功提交！</h1>";
echo "<br>";
echo "<h2>您的定订单信息如下：</h2>";
echo "<p>联系人：" . $_POST[client_name] . "</p>";
echo "<p>地址：" . $_POST[address] . "</p>";
echo "<p>联系电话：" . $_POST[phone_number] . "</p>";
echo "<p>瘦身组合数量：" . $_POST[amount_t1] . "</p>";
echo "<p>美颜组合数量：" . $_POST[amount_t2] . "</p>";
echo "<p>美颜组合数量：" . $_POST[amount_t3] . "</p>";
echo "<br>";
echo "<a href='http://www.chiguoguo.com.cn/'><img src='images/finish_button.png' alt='' /></a>";


mysql_close($con);
?>